---@diagnostic disable: undefined-global

local changmou = fk.CreateSkill{
  name = "lvshi__changmou",
  frequency = "skill",
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"

-- 使用viewas效果实现视为使用【里应外合】
changmou:addEffect("viewas", {
  name = "lvshi__changmou_viewas",
  card_filter = Util.FalseFunc,
  pattern = "lvshi__collaborate",  -- 视为使用里应外合
  view_as = function(self, cards)
    local card = Fk:cloneCard("lvshi__collaborate", Card.NoSuit, 0)
    card.skillName = changmou.name
    return card
  end,
  before_use = function(self, player, use)
    -- 记录使用前的手牌数量，用于判断是否获得牌
    local room = player.room
    local original_card_count = player:getHandcardNum()
    room:setPlayerMark(player, "lvshi__changmou-count", original_card_count)
    return true
  end,
  after_use = function(self, player, use)
    local room = player.room
    local original_count = player:getMark("lvshi__changmou-count") or 0
    local has_obtained_cards = player:getHandcardNum() > original_count
    -- 根据是否获得牌执行不同效果
    if has_obtained_cards then
      -- 获得了牌，可以选择失去体力
      if room:askToSkillInvoke(player, {
        skill_name = changmou.name,
        prompt = "#changmou-losehp"
      }) then
        room:loseHp(player, 1, changmou.name)
        room:setPlayerMark(player, "lvshi__changmou-phase", 1)
      end
    else
      local cards = {}
      for _, p in ipairs(use.tos) do
        table.insertTable(cards, DIY.getShownCards(p))
      end
      if #cards > 0 and room:askToSkillInvoke(player, { skill_name = changmou.name, prompt = "#changmou-restore" }) then
        local to_restore, choice = room:askToChooseCardsAndChoice(player, { cards = cards, skill_name = changmou.name, prompt = "#changmou-hide" })
        if to_restore then
          local owner = room:getCardOwner(to_restore[1])
          DIY.hideCards(owner, to_restore)
        end
        room:setPlayerMark(player, "lvshi__changmou-phase", 1)
      end
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(changmou.name, Player.HistoryPhase) <= player:getMark("lvshi__changmou-phase")
  end,
})


Fk:loadTranslationTable{
  ["lvshi__changmou"] = "唱谋",
  [":lvshi__changmou"] = "阶段技，你可视为使用【里应外合】，若你未以此法/以此法获得牌，你可暗置目标角色的一张牌/失去1点体力，然后此技能本回合改为限两次。",
  
  ["#changmou-invoke"] = "唱谋：是否视为使用【里应外合】？",
  ["#changmou-choose-target"] = "唱谋：选择一名角色作为【里应外合】的目标",
  ["#changmou-losehp"] = "唱谋：你可失去1点体力，令此技能改为此阶段限两次",
  ["#changmou-restore"] = "唱谋：你可暗置目标角色的一张牌，令此技能改为此阶段限两次",
  ["#changmou-hide"] = "唱谋：选择目标角色的一张牌",

  ["$lvshi__changmou1"] = "蛮人无计，退其兵甲，则可破之。",
  ["$lvshi__changmou2"] = "溃其志，安其民，而后归心。",
}


return changmou